package com.example.sherlockzhong.plush_v2.Subscriptions;

/*
 * Created by DosonLeung on 2014/8/30.
 */

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.example.sherlockzhong.plush_v2.Components.buttons.BackButton;
import com.example.sherlockzhong.plush_v2.Models.PushModel;
import com.example.sherlockzhong.plush_v2.R;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class OrgDetailActivity extends Activity {
    String[] itemName = {"title", "time", "content"};
    int[] itemId = {R.id.detail_title, R.id.detail_time, R.id.detail_content};
    JSONArray newsArray;
    ListView detailList;
    ArrayList<Map<String, Object>> itemList = new ArrayList<Map<String, Object>>();
    BackButton backButton;
    Bundle bundle;
    String OrgId;
    ImageView org_detail_refresh;
    private ProgressDialog pd;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            stopReflashDialog();
            if (msg.what == 0x123) {
                if (newsArray != null) {
                    initItemList(newsArray);
                }
            }
        }
    };

    private Thread getNewThread(){
        Thread OrgDetailThread = new Thread() {
            @Override
            public void run() {
                newsArray = PushModel.getPosterNews(OrgId);
                handler.sendEmptyMessage(0x123);
            }
        };
        return OrgDetailThread;
    }


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.org_detail_list);
        initWidget();
        if(bundle!=null)
        OrgId = bundle.getString("OrgId");//这里是社团的id(String)
        showReflashDialog("加载中...");
        getNewThread().start();
    }

    private void initItemList(JSONArray newsArray) {
        int Length = newsArray.length();
        JSONObject newsObject;
        itemList = new ArrayList<Map<String, Object>>();
        try {
            for (int i = 0; i < Length; i++) {
                newsObject = newsArray.getJSONObject(i);
                Map<String, Object> item = new HashMap<String, Object>();
                item.put("title", newsObject.getString("title"));
                item.put("time", newsObject.getString("create_time"));
                item.put("content", newsObject.getString("content"));
                itemList.add(item);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        SimpleAdapter SA = new SimpleAdapter(OrgDetailActivity.this, itemList, R.layout.org_detail_item, itemName, itemId);

        detailList.setAdapter(SA);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            this.result = intent.getExtras().getString("result");//得到新Activity 关闭后返回的数据
            stopReflashDialog();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void preActivity() {
        // 实例化 Bundle，设置需要传递的参数
        // 需要返回的数据存入到intent中
        if (this.getClass().equals(OrgDetailActivity.class)) {
            Intent intent = new Intent();
            intent.putExtra("result", "no");
            //设置返回数据
            this.setResult(Activity.RESULT_OK, intent);
        }
        stopReflashDialog();
        this.finish();
    }

    private void initWidget(){
        backButton = (BackButton) findViewById(R.id.detail_back);
        detailList = (ListView) findViewById(R.id.org_detial_list);
        org_detail_refresh = (ImageView) findViewById(R.id.org_detail_refresh);
        backButton.setApp(this);
        bundle = getIntent().getExtras();
        org_detail_refresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AnimationSet as = new AnimationSet(true);
                final float w = org_detail_refresh.getWidth() / 2;
                final float h = org_detail_refresh.getHeight() / 2;
                RotateAnimation ra = new RotateAnimation(0, 720, w, h);
                ra.setDuration(500);
                as.addAnimation(ra);
                org_detail_refresh.startAnimation(as);
                showReflashDialog("加载中...");
                getNewThread().start();
            }
        });
    }

    /**
     * 显示刷新的时候的等待进度条
     */
    public void showReflashDialog(String msg) {
        this.pd = new ProgressDialog(this);
        this.pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        this.pd.setCancelable(true);
        this.pd.setMessage(msg);
        this.pd.show();
    }

    /**
     * 结束刷新的时候的等待进度条
     */
    public void stopReflashDialog() {
        if (this.pd != null) {
            pd.dismiss();
        }
    }

    public void onDestory() {
        super.onDestroy();
        stopReflashDialog();
    }

}
